import java.util.Arrays;

public class InsertSort {
    public static void main(String[] args) {
        int[] elem = {16,3,2,5};
        insertSort(elem);
        System.out.println(Arrays.toString(elem));
    }
    //直接插入排序
    public static void insertSort(int[] array){
        if(array.length < 1){
            return;
        }
        int i = 1;
        int j = 0;
        while (i < array.length){
            j = i - 1;
            int tmp = array[i];
            while (j >= 0){
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else {
                    array[j+1] = tmp;
                    break;
                }
                j--;
            }
            array[j+1] = tmp;
            i++;
        }

    }
}
